package com.h3c.nem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.h3c.nem.dto.SdUploadFileDTO;
import com.h3c.nem.entity.SdAllWorker;
import com.h3c.nem.entity.SdUploadFile;
import com.h3c.nem.mapper.SdAllWorkerMapper;
import com.h3c.nem.mapper.SdUploadFileMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ISdUploadFileService;
import com.h3c.nem.utils.InitExampleOrderByUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: SdUploadFileServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-01-19
 * Description: sd_upload_file serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class SdUploadFileServiceImpl extends ServiceImpl<SdUploadFileMapper, SdUploadFile> implements ISdUploadFileService {

    @Resource
    private SdUploadFileMapper sdUploadFileMapper;

    @Autowired
    CommService commService;

    @Autowired
    SdAllWorkerMapper sdAllWorkerMapper;


    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<SdUploadFileDTO> selectFuzzyByDto(SdUploadFileDTO dto) {
        //List<SdUploadFile> list = sdUploadFileMapper.selectByExample(initFuzzyExample(dto));
        QueryWrapper<SdUploadFile> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(dto.getOperatorId())){
            queryWrapper.in("operator_id", Arrays.asList(dto.getOperatorId().split(",")));
        }
        if(dto.getBusiType()!=null){
            queryWrapper.eq("busi_type",dto.getBusiType());
        }
        queryWrapper.eq(!StringUtils.isEmpty(dto.getBusiId()),"busi_id",dto.getBusiId());
        List<SdUploadFile> fileList=sdUploadFileMapper.selectList(queryWrapper);
        //List<SdUploadFile> list = sdUploadFileMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),SdUploadFile.class));
        List<String> workerIdList = fileList.stream().map(SdUploadFile::getOperatorId).distinct().collect(Collectors.toList());
       // List<String> myList = workerIdList.stream().distinct().collect(Collectors.toList());
        Map<String,String> map=new HashMap<>();
        workerIdList.forEach(workerId->{
            map.put(workerId, sdAllWorkerMapper.selectOne(new QueryWrapper<SdAllWorker>().eq("worker_id",workerId)).getWorkerName());
        });
        fileList.forEach(file->{
            String operatorId=file.getOperatorId();
            file.setOperatorId(map.get(operatorId)+'/'+operatorId);
        });
        return JSONArray.parseArray(JSONArray.toJSONString(fileList), SdUploadFileDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<SdUploadFileDTO> selectByDto(SdUploadFileDTO dto) {
        //List<SdUploadFile> list = sdUploadFileMapper.selectByExample(initExample(dto));
        List<SdUploadFile> list = sdUploadFileMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),SdUploadFile.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), SdUploadFileDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(SdUploadFileDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = IdWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        return sdUploadFileMapper.insertSdUploadFile(JSONObject.parseObject(JSON.toJSONString(dto), SdUploadFile.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(SdUploadFileDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return sdUploadFileMapper.updateSdUploadFile(JSONObject.parseObject(JSON.toJSONString(dto), SdUploadFile.class));
            //return sdUploadFileMapper.updateByPrimaryKey(JSONObject.parseObject(JSON.toJSONString(dto), SdUploadFile.class));
        //} else {
        //    return 0;
        //}
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(SdUploadFileDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {

        commService.deleteSfsFile(dto.getFileId());
        return sdUploadFileMapper.delete(new QueryWrapper<SdUploadFile>().eq("file_id",dto.getFileId()));
        //return sdUploadFileMapper.deleteSdUploadFile(JSONObject.parseObject(JSON.toJSONString(dto), SdUploadFile.class));
        //return sdUploadFileMapper.deleteByPrimaryKey(dto.getId());
        //} else {
        //    return 0;
        //}
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(SdUploadFileDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(SdUploadFile.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getFileId() && !"".equals(dto.getFileId())) {
                criteriaName.andLike("FileId", "%" + dto.getFileId() + "%");
            }
            if (null != dto.getOperatorId() && !"".equals(dto.getOperatorId())) {
                criteriaName.andLike("OperatorId", "%" + dto.getOperatorId() + "%");
            }
            if (null != dto.getBusiType() && !"".equals(dto.getBusiType())) {
                criteriaName.andLike("BusiType", "%" + dto.getBusiType() + "%");
            }
            if (null != dto.getFileName() && !"".equals(dto.getFileName())) {
                criteriaName.andLike("FileName", "%" + dto.getFileName() + "%");
            }
            if (null != dto.getUploadTime() && !"".equals(dto.getUploadTime())) {
                criteriaName.andLike("UploadTime", "%" + dto.getUploadTime() + "%");
            }
            if (null != dto.getBusiId() && !"".equals(dto.getBusiId())) {
                criteriaName.andLike("BusiId", "%" + dto.getBusiId() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(SdUploadFileDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(SdUploadFile.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getFileId() && !"".equals(dto.getFileId())) {
                criteriaName.andEqualTo("FileId", dto.getFileId());
            }
            if (null != dto.getOperatorId() && !"".equals(dto.getOperatorId())) {
                criteriaName.andEqualTo("OperatorId", dto.getOperatorId());
            }
            if (null != dto.getBusiType() && !"".equals(dto.getBusiType())) {
                criteriaName.andEqualTo("BusiType", dto.getBusiType());
            }
            if (null != dto.getFileName() && !"".equals(dto.getFileName())) {
                criteriaName.andEqualTo("FileName", dto.getFileName());
            }
            if (null != dto.getUploadTime() && !"".equals(dto.getUploadTime())) {
                criteriaName.andEqualTo("UploadTime", dto.getUploadTime());
            }
            if (null != dto.getBusiId() && !"".equals(dto.getBusiId())) {
                criteriaName.andEqualTo("BusiId", dto.getBusiId());
            }
        }
        return example;
    }


}
